﻿namespace WMS6.Web.Outbound
{
    using System;
    using System.Collections.Generic;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.OutboundServiceRef;

    public class EncaseMent : BasePage
    {
        protected Button btnGoBack;
        protected Button btnSave;
        protected HtmlForm F7064;
        protected GridView gvdOrderDetail;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnOrderID;
        protected HtmlInputHidden hdnOwnerID;
        protected HtmlInputHidden hdnType;
        protected HtmlImage imgContainerType;
        protected Label lblAllocateRuleDetail;
        protected Label lblBarcode;
        protected Label lblContainerID;
        protected Label lblContainerType;
        protected Label lblHeight;
        protected Label lblLength;
        protected Label lblMsg;
        protected Label lblQty;
        protected Label lblRemark;
        protected Label lblSkuID;
        protected Label lblTotalQty;
        protected Label lblValue;
        protected Label lblVolume;
        protected Label lblWeight;
        protected Label lblWidth;
        protected HtmlLink mycss;
        protected Label one1;
        protected ScriptManager ScriptManager1;
        protected TextBox txtBarcode;
        protected TextBox txtContainerID;
        protected TextBox txtContainerType;
        protected TextBox txtHeight;
        protected TextBox txtLength;
        protected TextBox txtQty;
        protected TextBox txtRemark;
        protected TextBox txtSkuID;
        protected TextBox txtTotalQty;
        protected TextBox txtValue;
        protected TextBox txtVolume;
        protected TextBox txtWeight;
        protected TextBox txtWidth;

        protected void btnGoBack_Click(object sender, EventArgs e)
        {
            base.Response.Redirect(string.Format("Shipping.aspx?from={0}&ID={1}&Tab=3", base.UrlEncode(this.hdnFrom.Value), base.UrlEncode(this.hdnOrderID.Value)));
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtContainerID.Text))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F7600_002");
            }
            else if (string.IsNullOrEmpty(this.txtContainerType.Text))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F7600_003");
            }
            else if (this.hdnType.Value == "1")
            {
                this.UpdateData();
            }
            else
            {
                this.InserData();
            }
        }

        protected void gvdOrderDetail_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (e.Row.RowIndex + 1).ToString();
                ((Label) e.Row.FindControl("lblUnEncasedQty")).Attributes.Add("name", ((Label) e.Row.FindControl("lblUnEncasedQty")).Text);
                ((TextBox) e.Row.FindControl("txtInBoxQty")).Attributes.Add("onchange", "InBoxQtyCount('" + ((TextBox) e.Row.FindControl("txtInBoxQty")).ClientID + "','" + ((Label) e.Row.FindControl("lblEncasedQty")).ClientID + "','" + ((Label) e.Row.FindControl("lblUnEncasedQty")).ClientID + "','" + ((Label) e.Row.FindControl("lblPickedQty")).ClientID + "')");
            }
        }

        private void InserData()
        {
            List<ShipmentContainerInfo> ShipmentContainerList = new List<ShipmentContainerInfo>();
            ShipmentContainerInfo shipmentContainerInfo = new ShipmentContainerInfo {
                ContainerID = this.txtContainerID.Text,
                ContainerType = this.txtContainerType.Text,
                OrderID = this.hdnOrderID.Value
            };
            if (!string.IsNullOrEmpty(this.txtValue.Text))
            {
                shipmentContainerInfo.Value = Convert.ToDouble(this.txtValue.Text);
            }
            else
            {
                shipmentContainerInfo.Value = Convert.ToDouble("0");
            }
            if (!string.IsNullOrEmpty(this.txtVolume.Text))
            {
                shipmentContainerInfo.Volume = Convert.ToDouble(this.txtVolume.Text);
            }
            else
            {
                shipmentContainerInfo.Volume = Convert.ToDouble("0");
            }
            if (!string.IsNullOrEmpty(this.txtWeight.Text))
            {
                shipmentContainerInfo.Weight = Convert.ToDouble(this.txtWeight.Text);
            }
            else
            {
                shipmentContainerInfo.Weight = Convert.ToDouble("0");
            }
            if (!string.IsNullOrEmpty(this.txtWidth.Text))
            {
                shipmentContainerInfo.Width = Convert.ToDouble(this.txtWidth.Text);
            }
            else
            {
                shipmentContainerInfo.Width = Convert.ToDouble("0");
            }
            if (!string.IsNullOrEmpty(this.txtLength.Text))
            {
                shipmentContainerInfo.Length = Convert.ToDouble(this.txtLength.Text);
            }
            else
            {
                shipmentContainerInfo.Length = Convert.ToDouble("0");
            }
            shipmentContainerInfo.Remark = this.txtRemark.Text;
            shipmentContainerInfo.Qty = 0M;
            shipmentContainerInfo.TotalQty = 0M;
            shipmentContainerInfo.SkuID = "";
            shipmentContainerInfo.UomID = "";
            shipmentContainerInfo.OrderLineID = 0;
            shipmentContainerInfo.OwnerID = this.hdnOwnerID.Value;
            shipmentContainerInfo.MultiFlag = "Y";
            shipmentContainerInfo.IsActive = "N";
            shipmentContainerInfo.Status = "OK";
            shipmentContainerInfo.CreatedBy = base.UserInfo.UserID;
            shipmentContainerInfo.ExternOrderID = "";
            shipmentContainerInfo.ExternLineID = "";
            ShipmentContainerList.Add(shipmentContainerInfo);
            if (ShipmentContainerList.Count > 0)
            {
                try
                {
                    this.lblMsg.Text = string.Format(base.GetGlobalMsgResource("F7600_004"), OutboundServiceClientProxy.GetInstance().InsertShipmentContainer(ShipmentContainerList));
                    this.LoadData();
                }
                catch (Exception ex)
                {
                    this.lblMsg.Text = ex.Message;
                }
            }
        }

        private void LoadData()
        {
            this.hdnFrom.Value = base.Request.QueryString["from"];
            this.hdnOwnerID.Value = base.Request.QueryString["OwnerID"];
            this.hdnOrderID.Value = base.Request.QueryString["OrderID"];
            if (!string.IsNullOrEmpty(base.Request.QueryString["ContainerID"]) || !string.IsNullOrEmpty(this.txtContainerID.Text))
            {
                if (!string.IsNullOrEmpty(base.Request.QueryString["ContainerID"]))
                {
                    this.txtContainerID.Text = base.Request.QueryString["ContainerID"];
                }
                if (!string.IsNullOrEmpty(base.Request.QueryString["CType"]))
                {
                    this.txtContainerType.Text = base.Request.QueryString["CType"];
                }
                this.hdnType.Value = "1";
                this.textEnabledFalse();
                OrdersInfo orderInfo = OutboundServiceClientProxy.GetInstance().GetOrdersInfoAndDetailListByID(this.hdnOrderID.Value, this.txtContainerID.Text);
                base.DataEntityViewState = orderInfo;
                foreach (ShipmentContainerInfo info in orderInfo.ShipmentContainerInfos)
                {
                    if (info.ContainerID == this.txtContainerID.Text)
                    {
                        this.txtContainerType.Text = info.ContainerType;
                        this.txtRemark.Text = info.Remark;
                        this.txtHeight.Text = info.Height.ToString();
                        this.txtLength.Text = info.Length.ToString();
                        this.txtVolume.Text = info.Volume.ToString();
                        this.txtWeight.Text = info.Weight.ToString();
                        this.txtWidth.Text = info.Width.ToString();
                        this.txtValue.Text = info.Value.ToString();
                    }
                }
                base.BindData2GridView(this.gvdOrderDetail, orderInfo.OrderDetailInfos);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
            this.lblMsg.Text = "";
        }

        private void textEnabledFalse()
        {
            this.txtContainerID.Enabled = false;
            this.txtContainerType.Enabled = false;
            this.txtTotalQty.Enabled = false;
            this.txtRemark.Enabled = false;
            this.txtHeight.Enabled = false;
            this.txtLength.Enabled = false;
            this.txtVolume.Enabled = false;
            this.txtWeight.Enabled = false;
            this.txtWidth.Enabled = false;
            this.txtValue.Enabled = false;
            this.imgContainerType.Visible = false;
        }

        private void UpdateData()
        {
            List<ShipmentContainerInfo> ShipmentContainerList = new List<ShipmentContainerInfo>();
            foreach (GridViewRow row in this.gvdOrderDetail.Rows)
            {
                if (!string.IsNullOrEmpty(((TextBox) row.Cells[7].FindControl("txtInBoxQty")).Text))
                {
                    ShipmentContainerInfo shipmentContainerInfo = new ShipmentContainerInfo {
                        ContainerID = this.txtContainerID.Text,
                        ContainerType = this.txtContainerType.Text,
                        OrderID = this.hdnOrderID.Value
                    };
                    if (!string.IsNullOrEmpty(this.txtValue.Text))
                    {
                        shipmentContainerInfo.Value = Convert.ToDouble(this.txtValue.Text);
                    }
                    else
                    {
                        shipmentContainerInfo.Value = Convert.ToDouble("0");
                    }
                    if (!string.IsNullOrEmpty(this.txtVolume.Text))
                    {
                        shipmentContainerInfo.Volume = Convert.ToDouble(this.txtVolume.Text);
                    }
                    else
                    {
                        shipmentContainerInfo.Volume = Convert.ToDouble("0");
                    }
                    if (!string.IsNullOrEmpty(this.txtWeight.Text))
                    {
                        shipmentContainerInfo.Weight = Convert.ToDouble(this.txtWeight.Text);
                    }
                    else
                    {
                        shipmentContainerInfo.Weight = Convert.ToDouble("0");
                    }
                    if (!string.IsNullOrEmpty(this.txtWidth.Text))
                    {
                        shipmentContainerInfo.Width = Convert.ToDouble(this.txtWidth.Text);
                    }
                    else
                    {
                        shipmentContainerInfo.Width = Convert.ToDouble("0");
                    }
                    if (!string.IsNullOrEmpty(this.txtLength.Text))
                    {
                        shipmentContainerInfo.Length = Convert.ToDouble(this.txtLength.Text);
                    }
                    else
                    {
                        shipmentContainerInfo.Length = Convert.ToDouble("0");
                    }
                    shipmentContainerInfo.Remark = this.txtRemark.Text;
                    shipmentContainerInfo.Qty = Convert.ToDecimal(((TextBox) row.Cells[7].FindControl("txtInBoxQty")).Text);
                    shipmentContainerInfo.TotalQty += Convert.ToDecimal(shipmentContainerInfo.Qty);
                    shipmentContainerInfo.SkuID = row.Cells[2].Text;
                    shipmentContainerInfo.UomID = row.Cells[5].Text;
                    shipmentContainerInfo.OrderLineID = int.Parse(row.Cells[1].Text);
                    shipmentContainerInfo.OwnerID = this.hdnOwnerID.Value;
                    shipmentContainerInfo.MultiFlag = "Y";
                    shipmentContainerInfo.IsActive = "Y";
                    shipmentContainerInfo.Status = "OK";
                    shipmentContainerInfo.CreatedBy = base.UserInfo.UserID;
                    shipmentContainerInfo.ExternOrderID = "";
                    shipmentContainerInfo.ExternLineID = "";
                    ShipmentContainerList.Add(shipmentContainerInfo);
                }
            }
            if (ShipmentContainerList.Count > 0)
            {
                try
                {
                    this.lblMsg.Text = string.Format(base.GetGlobalMsgResource("F7600_005"), OutboundServiceClientProxy.GetInstance().UpdateShipmentContainer(ShipmentContainerList));
                    this.LoadData();
                }
                catch (Exception ex)
                {
                    this.lblMsg.Text = ex.Message;
                }
            }
        }
    }
}

